package com.share.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.exception.ServiceException;
import com.share.common.security.utils.SecurityUtils;
import com.share.device.domain.Cabinet;
import com.share.device.domain.CabinetSlot;
import com.share.device.domain.PowerBank;
import com.share.device.mapper.CabinetMapper;
import com.share.device.mapper.CabinetSlotMapper;
import com.share.device.mapper.PowerBankMapper;
import com.share.device.service.IPowerBankService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/10/11 下午2:16
 */
@Service
@RequiredArgsConstructor
public class PowerBankServiceImpl extends ServiceImpl<PowerBankMapper, PowerBank> implements IPowerBankService {

    private final PowerBankMapper powerBankMapper;
    private final CabinetSlotMapper cabinetSlotMapper;
    private final CabinetMapper cabinetMapper;

    /**
     * 查询充电宝列表
     *
     * @param powerBank
     * @return
     */
    @Override
    public List<PowerBank> selectPowerBankList(PowerBank powerBank) {
        return powerBankMapper.selectPowerBankList(powerBank);
    }

    /**
     * 添加充电宝
     *
     * @param powerBank
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int savePowerBank(PowerBank powerBank) {
        long count = this.count(new LambdaQueryWrapper<PowerBank>()
                .eq(PowerBank::getPowerBankNo, powerBank.getPowerBankNo()));
        if (count > 0) {
            throw new ServiceException("该充电宝编号已经存在");
        }
        powerBank.setElectricity(new BigDecimal("100"));
        return powerBankMapper.insert(powerBank);
    }

    /**
     * 修改充电宝信息
     *
     * @param powerBank
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updatePowerBank(PowerBank powerBank) {
        //获取旧数据
        PowerBank oldPowerBank = powerBankMapper.selectById(powerBank.getId());
        if (oldPowerBank == null) {
            throw new ServiceException("该充电宝不存在");
        }
        if (!ObjectUtils.isEmpty(oldPowerBank) && !"0".equals(oldPowerBank.getStatus())) {
            throw new ServiceException("该充电宝已经投放，无法修改");
        }

        //判断柜机编号是否改变
        if (!oldPowerBank.getPowerBankNo().equals(powerBank.getPowerBankNo())) {
            long count = this.count(new LambdaQueryWrapper<PowerBank>()
                    .eq(PowerBank::getPowerBankNo, powerBank.getPowerBankNo()));
            if (count > 0) {
                throw new ServiceException("该充电宝编号已经存在");
            }
        }

        return powerBankMapper.updateById(powerBank);
    }

    /**
     * 通过充电宝编号获取充电宝信息
     *
     * @param powerBankNo
     * @return
     */
    @Override
    public PowerBank getByPowerBankNo(String powerBankNo) {
        return powerBankMapper.selectOne(new LambdaQueryWrapper<PowerBank>().eq(PowerBank::getPowerBankNo, powerBankNo));
    }

    /**
     * 搜索未使用的充电宝
     *
     * @param keyword
     * @return
     */
    @Override
    public List<PowerBank> searchNoUseList(String keyword) {
        LambdaQueryWrapper<PowerBank> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(PowerBank::getPowerBankNo, keyword)
                .eq(PowerBank::getStatus, "0");
        return powerBankMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 设置充电宝数据
     *
     * @param params
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int setData(Map<String, Object> params) {
        String powerBankNo = (String) params.get("powerBankNo");
        Integer id = (Integer) params.get("slotId");
        PowerBank powerBank = powerBankMapper.selectOne(new LambdaQueryWrapper<PowerBank>().eq(PowerBank::getPowerBankNo, powerBankNo));
        CabinetSlot cabinetSlot = cabinetSlotMapper.selectById(id);
        if (powerBank == null) {
            throw new ServiceException("该充电宝不存在！");
        }
        if (cabinetSlot == null) {
            throw new ServiceException("该插槽不存在！");
        }

        powerBank.setStatus("1"); //0.未投放 1.可用 2.已租用 3.充电中 4.故障
        powerBank.setUpdateTime(new Date());
        powerBank.setUpdateBy(SecurityUtils.getUsername());
        powerBankMapper.updateById(powerBank);

        cabinetSlot.setPowerBankId(powerBank.getId());
        cabinetSlot.setStatus("1"); //0.空闲 1.占用 2.锁定
        cabinetSlot.setUpdateTime(new Date());
        cabinetSlot.setUpdateBy(SecurityUtils.getUsername());
        cabinetSlotMapper.updateById(cabinetSlot);

        Cabinet cabinet = cabinetMapper.selectOne(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getId, cabinetSlot.getCabinetId()));
        cabinet.setAvailableNum(cabinet.getAvailableNum() + 1);
        cabinet.setUsedSlots(cabinet.getUsedSlots() + 1);
        cabinet.setFreeSlots(cabinet.getFreeSlots() - 1);
        cabinet.setUpdateTime(new Date());
        cabinet.setUpdateBy(SecurityUtils.getUsername());
        cabinetMapper.updateById(cabinet);
        return 1;
    }
}
